/**
 * 
 */
package com.fita.admin.web.controller.coupon;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.core.web.utils.WebUtils;
import com.fita.admin.entity.content.CouponTemplateSearch;
import com.fita.admin.service.coupon.CouponService;
import com.fita.entity.coupon.CouponTemplate;
import com.fita.entity.coupon.DiscountType;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * 客户列表
 * 
 * @author LZJ
 *
 */
@Controller
@RequestMapping(path = "/coupon-template")
@MenuSetting("menu-coupon")
public class CouponTemplateController {
	
	 @Autowired
	 private CouponService<CouponTemplate> couponTemplateService;

	 private static final Logger logger = LoggerFactory.getLogger(CouponTemplateController.class);
	 
    /**
     * @param request 
     * @return
     */
    @GetMapping(path = "")
    @MenuSetting("submenu-coupon-template-management")
    public ModelAndView list(HttpServletRequest request, Page page) {
        
        ModelAndView modelAndView = new ModelAndView("coupon/coupon-template/coupon-templates-list");
       
        List<CouponTemplate> couponTemplates = couponTemplateService.listCouponTemplates(page);
        modelAndView.addObject("couponTemplates", couponTemplates);
        
        modelAndView.addObject("refer", "list");
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
        
        return modelAndView;
    }
    
    /**
     * @param batchId
     * @return
     */
    @GetMapping(path = "/{templateId}")
    public ModelAndView view(HttpServletRequest request,@PathVariable Long templateId) {
        
        ModelAndView modelAndView = new ModelAndView("coupon/coupon-template/coupon-template-view");   
        
        
        CouponTemplate couponTemplate = couponTemplateService.loadTemplate(templateId);
        
        modelAndView.addObject("couponTemplate", couponTemplate);
        
        return modelAndView;
    }
    
    
    /**
     * @return
     */
    @GetMapping(path = "/add")
    public ModelAndView add() {
        
        ModelAndView modelAndView = new ModelAndView("coupon/coupon-template/coupon-template-add");
        
        return modelAndView;
    }
    
    @RequestMapping( value = "/create", method = RequestMethod.POST)
    public @ResponseBody String onCreate(CouponTemplate couponTemplate, 
                                         @RequestParam(value = "inputAmount", required = false) BigDecimal inputAmount,
                                         @RequestParam(value = "inputMinAmount", required = false) BigDecimal inputMinAmount) {
        
    	HttpResult result = new HttpResult();
        
        try {
            //固定金额，不可转增
            couponTemplate.setDiscountType(DiscountType.FIXED_AMOUNT);
            couponTemplate.setGiveAvailable(false);
            couponTemplateService.createCouponTemplate(couponTemplate, inputAmount, inputMinAmount);
            result.success().message("新建优惠券模版成功！");
        
        } catch(Exception e) {
            e.printStackTrace();
            
            result.fail("500").message("新建优惠券模版失败！");
            
        }
        
        return JsonUtils.toJson(result);
    }
    
    
    
    /**
     * Display coupon template edit panel
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
    public ModelAndView editInfo(@PathVariable Long id) {
        
        ModelAndView modelAndView = new ModelAndView("coupon/coupon-template/coupon-template-edit");
        
        CouponTemplate couponTemplate = couponTemplateService.load(CouponTemplate.class, id);
        
        modelAndView.addObject("couponTemplate", couponTemplate);
        
        return modelAndView;
        
    }
    
    /**
     * Do edit coupon template
     * 
     * @param couponTemplate
     * @param inputName
     * @param inputAmount
     * @param inputMinAmount
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PATCH)
    public @ResponseBody String onEditInfo(@Preloading("id") CouponTemplate couponTemplate,
                                           @RequestParam("inputName") String inputName,
                                           @RequestParam(value = "inputAmount", required = false) BigDecimal inputAmount, 
                                           @RequestParam(value = "inputMinAmount", required = false) BigDecimal inputMinAmount) {

    	HttpResult result = new HttpResult();
        
        try {
            couponTemplateService.editCouponTemplate(couponTemplate, inputName, inputAmount, inputMinAmount);
            result.success().message("修改模版信息成功！");

        } catch (Exception e) {
            result.fail("500").message("修改模版信息失败！");
        }
        return JsonUtils.toJson(result);
    }
    
    /**
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/edit-description", method = RequestMethod.GET)
    public ModelAndView editDescription(@PathVariable Long id) {
        
        ModelAndView modelAndView = new ModelAndView("coupon/coupon-template/coupon-template-edit-description");
        
        CouponTemplate couponTemplate = couponTemplateService.load(CouponTemplate.class, id);
        
        modelAndView.addObject("id", id);
        modelAndView.addObject("description", couponTemplate.getDescription());
        
        return modelAndView;
        
    }
    
    
    /**
     * 
     * @param id
     * @param description
     * @return
     */
    @RequestMapping(value = "/{id}/edit-description", method = RequestMethod.PATCH)
    public @ResponseBody String onEditDescription(@PathVariable("id") Long id,
                                                  @RequestParam("description") String description) {

    	HttpResult result = new HttpResult();
        
        try {
            if(StringUtils.isBlank(description)) {
                result.fail("500").message("使用规则不能为空");
            } else {
                CouponTemplate couponTemplate = couponTemplateService.load(CouponTemplate.class, id);
                
                couponTemplateService.editCouponTemplateDescription(couponTemplate, description);
                result.success().message("修改使用规则成功！");
            }
        } catch (Exception e) {
            result.fail("500").message("修改使用规则失败！");
        }
        return JsonUtils.toJson(result);
    }
    
    /**
     * Display the search panel
     * 
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public ModelAndView search() {
        
        ModelAndView modelAndView = new ModelAndView("coupon/coupon-template/coupon-template-search");
        
        return modelAndView;
    }
    
    /**
     * Do search
     * 
     * @param couponTemplateSearch
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET, params = "filter")
    public ModelAndView onSearch(CouponTemplateSearch couponTemplateSearch, HttpServletRequest request, Page page) {
        
        ModelAndView modelAndView  = new ModelAndView("coupon/coupon-template/coupon-templates-list");
        
        List<CouponTemplate> couponTemplates = couponTemplateService.searchCouponTemplate(couponTemplateSearch, page);
        
        modelAndView.addObject("couponTemplates", couponTemplates);
        modelAndView.addObject("refer", "search");
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
        
        return modelAndView;
        
    }
    
}
